package com.source.lightfowl.barcodeMachineGrantAndRecovery.controller;

import com.source.lightfowl.VO.BarcodeMachineGrantVO;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.pojo.BarcodeMachineGrant;
import com.source.lightfowl.barcodeMachineGrantAndRecovery.service.BarcodeMachineGrantService;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description: 脚标机发放管理控制器
 * @Date: 2020.3.16
 */
@Api(value = "脚标机发放管理",tags = "脚标机发放管理")
@RestController
@CrossOrigin
@RequestMapping("/barcodeMachineGrant")
public class BarcodeMachineGrantController {

    @Autowired
    private BarcodeMachineGrantService barcodeMachineGrantService;

    @ApiOperation(value = "查询全部数据")
    //查找全部
    @GetMapping
    public MyResult findAll(){
        List<BarcodeMachineGrantVO> all = barcodeMachineGrantService.findAll();

        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找 分页
    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
    @GetMapping("/barcodeMachineGrantFindAllPage")
    public MyResult findAllPage(int page,int pageNum){

        Page<BarcodeMachineGrant> all = barcodeMachineGrantService.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public MyResult findById(@PathVariable Integer id){
        BarcodeMachineGrantVO house = barcodeMachineGrantService.findById(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据脚标机的id查找
    @ApiOperation(value = "根据脚标机的id查询")
    @GetMapping("/findByBarcodeMachineId/{id}")
    public MyResult findByBarcodeMachineId(@PathVariable Integer id){
        List<BarcodeMachineGrant> house = barcodeMachineGrantService.findByBarcodeMachineId(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }
    //增加 修改
    @ApiOperation(value = "增加/修改 数据 ")
    @PostMapping
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(BarcodeMachineGrant barcodeMachineGrant){

        barcodeMachineGrantService.save(barcodeMachineGrant);
        //List<BarcodeMachineGrant> all = barcodeMachineGrantService.findAll();
        /*if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");*/
        return MyResult.build(0,"成功");
    }

    //删除
    @ApiOperation("根据id删除数据")
    @DeleteMapping("/{id}")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(@PathVariable Integer id){

        barcodeMachineGrantService.deleteById(id);
        //List<BarcodeMachineGrantVO> all = barcodeMachineGrantService.findAll();
        return MyResult.build(0,"看是否少了删除的id");
    }

    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/barcodeMachineRecoveryDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的

        barcodeMachineGrantService.deleteBatch(ids);
        return MyResult.build(0,"删除成功");

    }


}
